package cn.rui.controller;

import cn.rui.common.ContentBean;
import cn.rui.common.ResponseBean;
import cn.rui.common.Restart;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 公共控制器
 *
 * @author 徽州大都督
 * @date 2021/6/16
 */
@RestController
@RequestMapping("common")
@Api(tags = "公共控制器模块")
public class CommonController {

    @Value("${default.img.address}")
    private String imgAddress;

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @GetMapping("getImage")
    @ApiOperation(value = "获取图片")
    public void getImage(HttpServletRequest request, HttpServletResponse response) {
        FileInputStream fis = null;

        response.setContentType ("image/gif");

        try {
            OutputStream out = response.getOutputStream ();

            //File file = new File ("D:" + File.separator + "timg.jpg");
            File file = new File (imgAddress);

            fis = new FileInputStream (file);

            byte[] b = new byte[fis.available ()];

            fis.read (b);

            out.write (b);

            out.flush ();

        } catch (Exception e) {
            e.printStackTrace ();

        } finally {
            if (fis != null) {
                try {
                    fis.close ();

                } catch (IOException e) {
                    e.printStackTrace ();

                }

            }
        }
    }

    @PostMapping("updateImg")
    @ApiOperation(value = "上传图片")
    public ResponseBean updateImg(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {

        //获取文件的原始名称
        String originalFilename = file.getOriginalFilename ();

        //获取原始名称的后缀
        String extension ="."+ FilenameUtils.getExtension (originalFilename);

        //生成新的文件名称
        //String newFileName = UUID.randomUUID ().toString ().replace ("-", "") + extension;
        //获取文件类型
        String type = file.getContentType ();
        if (!type.split ("/")[0].equalsIgnoreCase ("image")) {
            return ResponseBean.error (ContentBean.ErrorFileType,300);
        }

        File file1 = new File (imgAddress);
        file1.delete ();

        int index = imgAddress.lastIndexOf ("/");
        String substring = imgAddress.substring (0, index);
        File file2 = new File (substring);

        String newFileName = "123"+ extension;
        file.transferTo (new File (file2, newFileName));
        return ResponseBean.success (ContentBean.UploadSuccess);
    }

    @Autowired
    private Restart restart;

    @RequestMapping("test")
    public void test(){
        restart.startproject ();
    }
}
